/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com 

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef TRI_UTILS_H_
#define TRI_UTILS_H_

#include <time.h>
#include <iostream>
#include <stdio.h>
#include <string>

#define CLOCK_MODE 1
#define TIMER_MODE 2

namespace tri{
namespace utils{

/**
 * Do a system sleep for x amount of 
 * milli seconds
 * 
 * @param milli : Time to sleep in milli seconds
 */
void sleep(long milli);

bool is_pow2(int i);
int next_pow2(int i);

int string2int(const char * s );
float string2float(const char * s);
long string2long(const char * s);
std::string int2string(int i,int base = 10);


class timer{
public:
    timer();
    virtual ~timer();
    
    /**
     * reset the timer.
     */
    void reset();
    
    /**
     * Start the timer
     */
    bool start();
    
    /**
     * Stop the timer
     */
    void stop();
    
    
    /**
     * Get the elapsed time since start() was called
     * in seconds.
     */
    double getElapsedTime();
    
    /**
     * Get how far along the timer is since
     * reset was called. Inicated by a value from 0.0 -> 1.0.
     * 1.0 being completion
     */
    double getProgress();
    
    /**
     * Is the timer still running.
     */
    bool isRunning();
    
    /**
     * Set the timer size in seconds
     */
    void setDurationTime(double t);    
    
    /**
     * Set timer running mode
     * CLOCK_MODE or TIMER_MODE
     */
    void setMode(int mode);
    
    /**
     * Get current running mode 
     */
    int getMode();
    
private:
    time_t start_time;
    double duration;
    time_t end_time;
    int mode;
};

/**
 * Basic Bubble Sort Algoritm for sorting an array of unsigned int 
 * in set of 4 in ascending order. Order is detrermined by the 
 * index given by hotIndex .
 * 
 * @param a : start of array
 * @param size : Size of the array. Must be a multiple of 4.
 * @param hotIndex : Index that determines the groups position in the array.
 *         valued from 0-3.
 *               
 */
void bubble_sort_list_4_uint32(unsigned int *a,int size,int hotIndex=0);

/**
 * @see bubble_sort_list_4_uint32
 * 
 * Orders by decending.
 */
void bubble_sort_list_4_uint32_decend(unsigned int *a,int size,int hotIndex=0);

/**
 * Fit a rectangle to a given aspectratio 
 */
void fit_to_area(float src_w,float src_h,float dst_ar,float &dst_w, float &dst_h);

}
}

#endif /*UTILS_H_*/
